package com.yay.rental.ui;

import java.util.ArrayList;
import java.util.List;

import com.yay.rental.R;
import com.yay.rental.provider.CommentsService;

import model.Comment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;


/**
 * Get Started!
 * @author team@getstarted.com.ua
 */
public class CommentsActivity  extends Activity {
	private CommentsAdapter mAdapter;
	private CommentsService mService;
	
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comments);
 
        GridView gridView = (GridView) findViewById(android.R.id.list);
		mAdapter = new CommentsAdapter(this); 
		mService = new CommentsService();
		mAdapter.getComments().addAll(mService.listComment(CommentsService.rentalId));
		gridView.setAdapter(mAdapter);
		gridView.setTranscriptMode(2);
    }

	
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mAdapter = null;
        mService = null;
        
    }
    
    /** Handle "back" title-bar action. */
    public void onBackClick(View v) {
    	finish();
    }
    
    /** Handle "send" title-bar action. */
    public void onSendClick(View v) {
    	EditText edtComment = (EditText) findViewById(R.id.edtComment);
		String text = edtComment.getText().toString();
    	if(text != null && !TextUtils.isEmpty(text.trim())) {
    		new CommentsService().saveComment(text.trim(), CommentsService.rentalId);
    		finish();
	    	Intent myIntent = new Intent(CommentsActivity.this, CommentsActivity.class);
	    	startActivity(myIntent);	    			
    	}
    }
    
    class CommentsAdapter extends BaseAdapter {
    	private static final long serialVersionUID = -5623354315863298693L;
    	private List<Comment> comments;
    	private Context mContext;
        
    	
        public CommentsAdapter(Context c) {
            this.mContext = c;
        }

        public List<Comment> getComments() {
        	if(comments == null) {
        		comments = new ArrayList<Comment>();
        	}
        	return comments;
        }
        
        public int getCount() {
            return comments.size();
        }

        public Comment getItem(int position) {
            return comments.get(position);
        }

        public long getItemId(int position) {
            return position;
        }

    	//private DateFormat in = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    	//private DateFormat out = new SimpleDateFormat("dd.MM HH:mm");
    	
        public View getView(int position, View convertView, ViewGroup parent) {
        	final Comment comment = comments.get(position);
        	
        	LinearLayout hor = new LinearLayout(mContext);
        	hor.setOrientation(LinearLayout.HORIZONTAL);
        	hor.setPadding(0,5,0,5);
        	
        	LinearLayout layout = new LinearLayout(mContext);
        	layout.setOrientation(LinearLayout.VERTICAL);
        	
	    	TextView timestamp = new TextView(mContext);
	    	timestamp.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));
	    	String time = "";
	    	/*try {  
	    		Date date = (Date)in.parse(comment.getTimestamp());
	    		time = out.format(date);
	    	} catch(ParseException e) {
	    		Log.e(TAG, e.toString());
	    	}*/
	    	timestamp.setText(time);
	    	timestamp.setTextSize(12f);
	    	timestamp.setTextColor(mContext.getResources().getColor(R.color.background4));
	    	timestamp.setGravity(Gravity.CENTER_VERTICAL);
	    	timestamp.setPadding(5,0,5,0);
	    	layout.addView(timestamp);
        	
            TextView text = new TextView(mContext);
            text.setTextColor(mContext.getResources().getColor(R.color.foreground2));
            text.setGravity(Gravity.CENTER_VERTICAL);
        	text.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));
        	String value = comment.getText();
        	text.setText(value);
            text.setTextSize(16f);
            text.setPadding(5,0,5,0);
            layout.addView(text);
        	
            hor.addView(layout);
            
            return hor;
        }
    }
    
}
